import { listPmsProductCategory } from "@/api/pms/productCategory";
import { allBrand } from "@/api/pms/brand";
import { areaSelect } from "@/api/system/common";
import { allMember } from "@/api/ums/member";
import { allSeacQualityInspectorMember } from "@/api/ums/seacQualityInspectorMember";
import { regionList } from "@/utils/region";
import { listSeaCucumberOrigin } from "@/api/pms/seaCucumberOrigin";
import { listSpec } from "@/api/pms/spec";

const state = {
  productCategories: [],
  brandList: [],
  areaSelect: [],
  regionList: [],
  seaCucumberOriginList: [],
  memberList: [],
  qualityInspectorMemberList: [],
  specList: []
};
const mutations = {
  SET_PRODUCT_CATEGORIES: (state, value) => {
    state.productCategories = value;
  },
  SET_areaSelect: (state, value) => {
    state.areaSelect = value;
  },
  SET_BRAND_LIST: (state, value) => {
    state.brandList = value;
  },
  SET_QUALITYINSPECTORMEMBERLIST_LIST: (state, value) => {
    state.qualityInspectorMemberList = value;
  },
  SET_MEMBER_LIST: (state, value) => {
    state.memberList = value;
  },
  SET_REGION_LIST: (state, value) => {
    state.regionList = value;
  },
  SET_SEA_CUCUMBER_ORIGION_LIST: (state, value) => {
    state.seaCucumberOriginList = value;
  },
  SET_SPEC_LIST: (state, value) => {
    state.specList = value;
  },
};
const actions = {
  loadProductCategories({ commit, state }, force = false) {
    if (!force && state.productCategories.length > 0) {
      return Promise.resolve();
    }
    return listPmsProductCategory({}).then((res) => {
      commit("SET_PRODUCT_CATEGORIES", res);
    });
  },
  loadAreaSelect({ commit, state }, force = false) {
    if (!force && state.areaSelect.length > 0) {
      return Promise.resolve();
    }
    return areaSelect({}, {}).then((res) => {
      commit("SET_areaSelect", res.data);
    });
  },
  loadRegionList(
    { commit, state },
    levelType = null,
    parentId = null,
    force = false
  ) {
    if (!force && state.regionList.length > 0) {
      return Promise.resolve();
    }
    return regionList(levelType, parentId).then((res) => {
      commit("SET_REGION_LIST", res);
    });
  },
  loadSeaCucumberOriginList({ commit, state }, force = false) {
    if (!force && state.seaCucumberOriginList.length > 0) {
      return Promise.resolve();
    }
    return listSeaCucumberOrigin({}, null).then((res) => {
      commit("SET_SEA_CUCUMBER_ORIGION_LIST", res.content);
    });
  },
  loadBrandList({ commit, state }, force = false) {
    if (!force && state.brandList.length > 0) {
      return Promise.resolve();
    }
    return allBrand({}).then((res) => {
      commit("SET_BRAND_LIST", res);
    });
  },
  loadMemberList({ commit, state }, force = false) {
    if (!force && state.memberList.length > 0) {
      return Promise.resolve();
    }
    return allMember({}).then((res) => {
      commit("SET_MEMBER_LIST", res);
    });
  },
  loadQualityInspectorMemberList({ commit, state }, force = false) {
    if (!force && state.qualityInspectorMemberList.length > 0) {
      return Promise.resolve();
    }
    return allSeacQualityInspectorMember({}).then((res) => {
      commit("SET_QUALITYINSPECTORMEMBERLIST_LIST", res);
    });
  },
  loadSpecList({ commit, state }, force = false) {
    if (!force && state.specList.length > 0) {
      return Promise.resolve();
    }
    return listSpec({}).then((res) => {
      commit("SET_SPEC_LIST", res.content);
    });
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
